package com.android.aircondition.view;


import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.aircondition.R;
import com.android.aircondition.service.GreeService;

public class ModeZhireView extends RelativeLayout{
	
	private ImageView sunImv;
	private ImageView haloImv;
	private ImageView lightImv;
	
	private GreeService service;
	
	public ModeZhireView(Context context) {
		super(context,null);
		initData(context);
	}

	public ModeZhireView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initData(context);
		
	}

	private void initData(Context context){
		
		service = new GreeService(context);
		
		DisplayMetrics dm = new DisplayMetrics();  
		dm = this.getResources().getDisplayMetrics();  
		// 获得屏幕宽度  
		int screenWidth = dm.widthPixels;  
		// 获得屏幕高度  
		int screenHeight = dm.heightPixels;  
		
		service = new GreeService(context);

		sunImv = new ImageView(context);
		LayoutParams sunParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		sunParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		sunImv.setBackgroundResource(R.drawable.zhire_sun);
		sunImv.setLayoutParams(sunParams);

		haloImv = new ImageView(context);
		LayoutParams haloParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		haloParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		haloImv.setBackgroundResource(R.drawable.zhire_halo);
		haloImv.setLayoutParams(sunParams);

		lightImv = new ImageView(context);
		LayoutParams lightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lightParams.leftMargin = screenWidth/3;
		lightParams.topMargin  = screenHeight/4;
		
		lightImv.setImageResource(R.drawable.zhire_light);
		lightImv.setLayoutParams(lightParams);
		lightImv.setVisibility(View.GONE);
		
		this.addView(sunImv);
		this.addView(haloImv);
		this.addView(lightImv);
		
	}
	
	public void startZhireAnimation(){
		Animation scaleAnimation  = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
		scaleAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				
				Animation scaleAnimationLight1  = new ScaleAnimation(0.9f, 1.2f, 0.9f, 1.2f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
				Animation scaleAnimationLight2  = new ScaleAnimation(1.2f, 0.9f, 1.1f, 0.9f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
				Animation scaleAnimationLight3  = new ScaleAnimation(0.9f, 1.2f, 0.9f, 1.2f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
				Animation scaleAnimationLight4  = new ScaleAnimation(1.2f, 0.8f, 1.1f, 0.8f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
				
				AnimationSet haloAnimaSet = new AnimationSet(true);
				
				scaleAnimationLight1.setInterpolator(new LinearInterpolator());
				scaleAnimationLight2.setInterpolator(new LinearInterpolator());
				scaleAnimationLight3.setInterpolator(new LinearInterpolator());
				scaleAnimationLight4.setInterpolator(new LinearInterpolator());
				
				scaleAnimationLight1.setDuration(1000);
				scaleAnimationLight2.setDuration(1000);
				scaleAnimationLight3.setDuration(1000);
				scaleAnimationLight4.setDuration(1000);
				
				scaleAnimationLight1.setStartOffset(0);
				scaleAnimationLight2.setStartOffset(1000);
				scaleAnimationLight3.setStartOffset(2000);
				scaleAnimationLight4.setStartOffset(3000);
				
				haloAnimaSet.addAnimation(scaleAnimationLight1);
				haloAnimaSet.addAnimation(scaleAnimationLight2);
				haloAnimaSet.addAnimation(scaleAnimationLight3);
				haloAnimaSet.addAnimation(scaleAnimationLight4);
				
				haloImv.setAnimation(haloAnimaSet);
				haloAnimaSet.start();
				
				AnimationSet lightAnimaSet = new AnimationSet(true);
				lightImv.setVisibility(View.VISIBLE);
				RotateAnimation lightAnimation1 = new RotateAnimation(0.0f, +30.0f, 10f, 10f);     
				RotateAnimation lightAnimation2 = new RotateAnimation(0.0f, -30.0f, 10f, 10f);
				RotateAnimation lightAnimation3 = new RotateAnimation(0.0f, +30.0f, 10f, 10f);     
				RotateAnimation lightAnimation4 = new RotateAnimation(0.0f, -30.0f, 10f, 10f);
				
				lightAnimation1.setDuration (1000);
				lightAnimation2.setDuration (1000);
				lightAnimation3.setDuration (1000);
				lightAnimation4.setDuration (1000);
				
				lightAnimation1.setStartOffset(0);
				lightAnimation2.setStartOffset(1000);
				lightAnimation3.setStartOffset(2000);
				lightAnimation4.setStartOffset(3000);
				
				lightAnimation1.setInterpolator(new LinearInterpolator());
				lightAnimation2.setInterpolator(new LinearInterpolator());
				lightAnimation3.setInterpolator(new LinearInterpolator());
				lightAnimation4.setInterpolator(new LinearInterpolator());
				
				lightAnimaSet.addAnimation(lightAnimation1);
				lightAnimaSet.addAnimation(lightAnimation2);
				lightAnimaSet.addAnimation(lightAnimation3);
				lightAnimaSet.addAnimation(lightAnimation4);
				
				lightImv.setAnimation(lightAnimaSet);
				lightAnimaSet.start();
			}
		});
		scaleAnimation.setDuration(2000);
		sunImv.setAnimation(scaleAnimation);
		haloImv.setAnimation(scaleAnimation);
		scaleAnimation.start();
		
	}
	
	@Override
	public void setVisibility(int visibility) {
		if(visibility == View.GONE){
			lightImv.setVisibility(View.GONE);
		}
		super.setVisibility(visibility);
	}
	
}
